package com.jiexin.controller;

import com.google.common.collect.Maps;
import com.jiexin.beans.PageQuery;
import com.jiexin.beans.PageResult;
import com.jiexin.common.JsonData;
import com.jiexin.dto.AclModuleLevelDto;
import com.jiexin.model.SysRole;
import com.jiexin.model.SysUser;
import com.jiexin.param.DeptParam;
import com.jiexin.param.UserParam;
import com.jiexin.service.SysRoleUserService;
import com.jiexin.service.SysTreeService;
import com.jiexin.service.SysUserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @Author: Jay_Liu
 * @Description: 用户
 * @Date: Created in 10:51 2018/4/6 0006
 * @Modified By:
 */
@Controller
@RequestMapping("/sys/user")
public class SysUserController {

    @Resource
    SysUserService sysUserService;

    @Resource
    SysRoleUserService sysRoleUserService;

    @Resource
    SysTreeService sysTreeService;

    @RequestMapping("/noAuth.page")
    //保存用户
    public ModelAndView noAuth() {
        return new ModelAndView("noAuth");
    }

    @RequestMapping("/save.json")
    @ResponseBody
    //保存用户
    public JsonData saveUser(UserParam userParam) {
        sysUserService.save(userParam);
        return JsonData.success();
    }

    @RequestMapping("/update.json")
    @ResponseBody
    //更新用户
    public JsonData updateUser(UserParam userParam) {
        sysUserService.update(userParam);
        return JsonData.success();
    }

    @RequestMapping("/page.json")
    @ResponseBody
    public JsonData page(@RequestParam("deptId") Integer deptId, PageQuery pageQuery) {
        PageResult<SysUser> result = sysUserService.getPageByDeptId(deptId, pageQuery);
        return JsonData.success(result);
    }

    @RequestMapping("/aclsAndRoles.json")
    @ResponseBody
    public JsonData aclsAndRoles(@RequestParam("userId") Integer userId) {
        //获取该用户下的权限模块与权限形成的树形结构
        List<AclModuleLevelDto> aclModuleLevelDtoList = sysTreeService.userAclTree(userId);
        //获取该用户所有的角色
        List<SysRole> roleList = sysRoleUserService.getRoleListByUserId(userId);
        //Map结果
        Map<String,Object> resultMap = Maps.newHashMap();
        resultMap.put("acls", aclModuleLevelDtoList);
        resultMap.put("roles", roleList);
        return JsonData.success(resultMap);
    }
}
